package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Cart;
import services.ServiceCart;

public class ServletResetCart extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        ServiceCart serviceCart = new ServiceCart();

        HttpSession session = request.getSession();
        String userIdsession = (String) session.getAttribute("userId");
        int userId = Integer.parseInt(userIdsession);
        List<Cart> carts = serviceCart.findCartByUserId(userId);

        for (Cart cart : carts) {
            int productId = cart.getProduct().getId();
            serviceCart.removeProductCart(userId, productId);
        }

        out.print("<h4>Your cart is currently empty</h4>");
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }
}
